package com.codi.api.controller.organ;

import com.codi.api.controller.BaseController;
import com.codi.api.security.UserUtil;
import com.codi.base.exception.BaseAppException;
import com.codi.base.log.annotation.Loggable;
import com.codi.base.util.StringUtil;
import com.codi.bus.constant.GlobalConstant;
import com.codi.bus.core.domain.User;
import com.codi.bus.core.resultModel.*;
import com.codi.bus.core.service.QueryService;
import com.codi.bus.core.service.organ.OrganAssetsService;
import com.codi.message.util.MessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by asus on 2018/5/8.
 * 资产
 */
@Slf4j
@RestController
@RequestMapping("/organ/assets")
public class OrganAssetsController extends BaseController{
    @Autowired
    private OrganAssetsService organAssetsService;

    @Autowired
    private QueryService queryService;

    /**
     * 全部资产
     * @param profileId
     * @return
     */
    @Loggable
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult assetsQuery(String profileId) {

        AssetsResult result = null;
        Long pId = Long.valueOf(profileId);
        result = organAssetsService.queryOrganAssets(UserUtil.getLoginUserId(),pId);
        if (result.getSuccess() == false && StringUtil.isEmpty(result.getErrorMessage())) {
            result.setErrorMessage(MessageUtil.getErrorMessage(result.getErrorCode(), null));
        }

        return result;
    }

    /**
     * 瑞富宝资产
     * @param profileId
     * @return
     * @throws BaseAppException
     */
    @Loggable
    @RequestMapping(value = "wallet", method = RequestMethod.GET)
    @ResponseBody
    public WalletDetailResultV2 getWalletAssets(String profileId) throws BaseAppException {
        Long userId = UserUtil.getLoginUserId();
        Long pId = Long.valueOf(profileId);
        WalletDetailResultV2 result = organAssetsService.getOrganWallet(userId,pId);
        return result;
    }

    /**
     * 获取单个t0基金的详情
     * 4.6.0 不再使用
     * @param fundCode
     * @return
     * @throws BaseAppException
     */
    @Loggable
    @RequestMapping(value = "walletDetail", method = RequestMethod.GET)
    @ResponseBody
    public WalletDetailResult detail(String fundCode, String profileId) throws BaseAppException {
        Long userId = UserUtil.getLoginUserId();
        try{
            Long pId = Long.valueOf(profileId);
            WalletDetailResult result = organAssetsService.getOrganWalletDetailByFundCode(userId,fundCode,pId);
            result.setRecommendedFundModel(queryService.queryRecommendedFund());//设置推荐基金
            return result;
        }
        catch(Exception e){e.printStackTrace();}

        return null;
    }

    /**
     * t0提现
     * 4.6.0 不再使用
     * @param fundCode
     * @return
     *
     */
    @Loggable
    @GetMapping("/cashChangeInfo/v2")
    @ResponseBody
    public BaseResult queryCashChangeInfoV2(String fundCode, String profileId) {
        Long pId = Long.valueOf(profileId);
        return organAssetsService.getWalletCashChangeDetails(UserUtil.getLoginUserId(), fundCode, pId);
    }

    /**
     * 查询资产，过滤掉T0基金和组合
     *
     * @param requestNum
     * @param queryFlag
     * @param beginNum
     * @param sortDirection
     */
    @RequestMapping(value = "/fund", method = RequestMethod.GET)
    @ResponseBody
    @Loggable
    public BaseResult shareQuery(@RequestParam Integer requestNum,
                                 @RequestParam Integer queryFlag,
                                 @RequestParam Integer beginNum,
                                 @RequestParam String sortDirection,
                                 String profileId
                                 ) {

        ShareListResult result = null;
        Long pId = Long.valueOf(profileId);
        result = organAssetsService.queryOrganSharesWithoutPort(UserUtil.getLoginUserId(), requestNum, queryFlag, beginNum,
            sortDirection, true,pId);
        if (result.getSuccess() == false && StringUtil.isEmpty(result.getErrorMessage())) {
            result.setErrorMessage(MessageUtil.getErrorMessage(result.getErrorCode(), null));
        }

        return result;
    }

    /**
     * 查询机构基金收益明细
     *
     * @throws Exception
     */
    @Loggable
    @GetMapping("/profit")
    @ResponseBody
    public BaseResult queryProfit( String fundCode, String type, Integer beginNum, Integer requestNum, String profileId) throws Exception {
        // 查询基金
        Long userId = UserUtil.getLoginUserId();
        Long pId = Long.valueOf(profileId);
        BaseResult result = organAssetsService.getProfit(userId,  fundCode, type, beginNum, requestNum, pId);

        return result;
    }

    /**
     * 累计收益 图表数据
     *
     * @throws Exception
     */
    @Loggable
    @GetMapping("/accumulate")
    @ResponseBody
    public BaseResult queryAccumulate(String fundCode, String type, String profileId) throws Exception {
        // 查询基金
        Long userId = UserUtil.getLoginUserId();
        Long pId = Long.valueOf(profileId);
        BaseResult result = organAssetsService.getAccum(userId,  fundCode, type, pId);

        return result;
    }

    /**
     * 查询单个基金资产
     * 企业宝和 普通基金共用
     */
    @RequestMapping(value = "/fundshare", method = RequestMethod.GET)
    @ResponseBody
    @Loggable
    public BaseResult shareQuery(String fundCode) {

        BaseResult result = null;


        result = organAssetsService.queryOrganFundShares(UserUtil.getLoginUserId(),fundCode,UserUtil.getProfileId());
        if (result.getSuccess() == false && StringUtil.isEmpty(result.getErrorMessage())) {
            result.setErrorMessage(MessageUtil.getErrorMessage(result.getErrorCode(), null));
        }

        return result;
    }
}
